﻿Imports System.Runtime.Serialization
Imports System.Web.Script.Serialization

Namespace WebService

    <DataContract()> _
    Public MustInherit Class PageItem

        Private mID As Long
        Private mDOB As DateTime
        Private mSource As String
        Private mTag As String

        Protected MustOverride Function GetPageTypeName() As String
        Protected MustOverride Function GetPrimaryValue() As String
        Protected MustOverride Function CopyFrom(ByVal page As PageItem) As Boolean
        Public MustOverride Function Validate() As Boolean

        ' -------------------------------------------------------------------------
        ' Property Definitions
        ' -------------------------------------------------------------------------
        '
        ''' <summary>
        ''' Property to retrieve the Unique ID for this Page Item.
        ''' </summary>
        <DataMember()> _
        Public Property ID As Long
            Get
                Return mID
            End Get
            Set(ByVal value As Long)
                mID = value
            End Set
        End Property

        ''' <summary>
        ''' Read-Write Property to retrieve the Client Tag for this Page Item.
        ''' </summary>
        <DataMember()> _
        Public Property Tag As String
            Get
                Return mTag
            End Get
            Set(ByVal value As String)
                mTag = value
            End Set
        End Property

        ''' <summary>
        ''' Read-Only Property to retrieve the creation time of this Page Item.
        ''' </summary>
        <DataMember()> _
        Public Property CreationTime As DateTime
            Get
                Return mDOB
            End Get
            Set(ByVal value As DateTime)
                mDOB = value
            End Set
        End Property

        ''' <summary>
        ''' Read-Only Property to retrieve the Page Type (Eg. Parent, Driver, Team, etc).
        ''' </summary>
        <DataMember()> _
        Public Property Type As String
            Get
                Return GetPageTypeName()
            End Get
            Set(ByVal value As String)
                '' Ignore (required only for Serialization)
            End Set
        End Property

        ''' <summary>
        ''' Read-Only Property to retrieve the Primary Value string for this Page. 
        ''' This is a uniquely identifying string for the Page among this Type of Page.
        ''' It is usually made up of one or more Properties values that when combined can identify
        ''' uniqueness of page.  This is used for 'SameAs' comparisions and page logging.
        ''' </summary>
        <DataMember()> _
        <ScriptIgnore()> _
        Public Property Primary As String
            Get
                Return GetPrimaryValue()
            End Get
            Set(ByVal value As String)
                '' Ignore (required only for Serialization)
            End Set
        End Property

        ''' <summary>
        ''' Read-Only Property to retrieve the source of this Page (client device).
        ''' </summary>
        <DataMember()> _
        Public Property Source As String
            Get
                Return mSource
            End Get
            Set(ByVal value As String)
                mSource = value
            End Set
        End Property

        Public Overridable Sub ModifyWith(ByVal page As PageItem)
            ' Only modify mutable properties, those that can be changed once 
            ' page has been created.
            If (CopyFrom(page)) Then
                If (hasValue(page.Tag)) Then
                    Me.Tag = page.Tag
                End If
            End If
        End Sub

        Protected Function hasValue(ByVal value As String) As Boolean
            Return (value IsNot Nothing AndAlso value.Length > 0)
        End Function

        Public Overridable Function SameAs(ByVal other As PageItem) As Boolean
            Dim rv = False
            If ((other IsNot Nothing) AndAlso (Me.GetType = other.GetType)) Then
                rv = (Me.Primary.ToLower.Equals(other.Primary.ToLower))
            End If
            Return rv
        End Function

        Public Overridable Function UserPropertiesMatch(ByVal other As PageItem) As Boolean
            Dim rv = False
            If (other IsNot Nothing) Then
                rv = (Me.Primary = other.Primary AndAlso
                      Me.Tag = Me.Tag AndAlso
                      Me.Type = other.Type)
            End If
            Return rv
        End Function


    End Class

End Namespace